package com.ews.onlineexchange.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.PostLoad;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.ews.onlineexchange.util.SystemUUIDUtil;

/**
 * 实体名称：资质证书 说明：从属于某中介公司的，真是存在的资质
 * 
 * @author admin
 *
 */
@Entity
public class QualificationCert extends DataEntity {
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	
	private Boolean					auditResult;			// 审核结果
	private Date					auditTime;				// 审核时间
	private RejectRecord			currRejectRecord;		// 最近一次驳回原因
	private List<RejectRecord> 		historyRejectRecords = new ArrayList<>(); // 驳回历史
	private List<QualificationTemplate>	qualificationTemplates = new ArrayList<QualificationTemplate>();
	private ServiceSupplier			serviceSupplier;
	private boolean					valid;					// 是否有效
	private Date					endingValidityDate;		// 截至有效期
	private Date					approveDate;
	private String					approveLicenseNo;
	private String					contactor;
	private String 					contactorPhoneNumber;
	private String 					approveDepartmentName;
	private Date					allotedDate;
	private List<Image>				certPhotos = new ArrayList<>();
	private QualificationClass		qualificationClass;
	private boolean					hidden;
	private ProfessionUser   		professionUser;
	
	private String 					certIndex;
	
	public Boolean getAuditResult() {
		return auditResult;
	}

	public void setAuditResult(Boolean auditResult) {
		this.auditResult = auditResult;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getAuditTime() {
		return auditTime;
	}

	public void setAuditTime(Date auditTime) {
		this.auditTime = auditTime;
	}

	@OneToOne(cascade = CascadeType.ALL)
	public RejectRecord getCurrRejectRecord() {
		return currRejectRecord;
	}

	public void setCurrRejectRecord(RejectRecord currRejectRecord) {
		this.currRejectRecord = currRejectRecord;
	}
	
	
	
	@OneToMany(cascade = CascadeType.ALL)
	@OrderBy("createDate DESC")
	public List<RejectRecord> getHistoryRejectRecords() {
		return historyRejectRecords;
	}

	public void setHistoryRejectRecords(List<RejectRecord> historyRejectRecords) {
		this.historyRejectRecords = historyRejectRecords;
	}

	@OneToMany(cascade = {CascadeType.ALL})
	@Size(min = 1)
	@JoinTable(name = "CERT_TEMPLATES")
	public List<QualificationTemplate> getQualificationTemplates() {
		return qualificationTemplates;
	}

	public void setQualificationTemplates(List<QualificationTemplate> qualificationTemplates) {
		this.qualificationTemplates = qualificationTemplates;
	}

	@ManyToOne
	@NotNull
	public ServiceSupplier getServiceSupplier() {
		return serviceSupplier;
	}

	public void setServiceSupplier(ServiceSupplier serviceSupplier) {
		this.serviceSupplier = serviceSupplier;
	}
	
	
	@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE } )
	public ProfessionUser getProfessionUser() {
		return professionUser;
	}

	public void setProfessionUser(ProfessionUser professionUser) {
		this.professionUser = professionUser;
	}

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	@Temporal(TemporalType.DATE)	
	public Date getEndingValidityDate() {
		return endingValidityDate;
	}

	public void setEndingValidityDate(Date endingValidityDate) {
		this.endingValidityDate = endingValidityDate;
	}

	@OneToOne(cascade= {CascadeType.MERGE})
	public QualificationClass getQualificationClass() {
		return qualificationClass;
	}

	public void setQualificationClass(QualificationClass qualificationClass) {
		this.qualificationClass = qualificationClass;
	}

	@Temporal(TemporalType.DATE)
	@NotNull
	public Date getApproveDate() {
		return approveDate;
	}

	public void setApproveDate(Date approveDate) {
		this.approveDate = approveDate;
	}

	@NotNull
	public String getApproveLicenseNo() {
		return approveLicenseNo;
	}

	public void setApproveLicenseNo(String approveLicenseNo) {
		this.approveLicenseNo = approveLicenseNo;
	}

	@NotNull
	public String getContactor() {
		return contactor;
	}

	public void setContactor(String contactor) {
		this.contactor = contactor;
	}

	@NotNull
	public String getContactorPhoneNumber() {
		return contactorPhoneNumber;
	}

	public void setContactorPhoneNumber(String contactorPhoneNumber) {
		this.contactorPhoneNumber = contactorPhoneNumber;
	}

	@NotNull
	public String getApproveDepartmentName() {
		return approveDepartmentName;
	}

	public void setApproveDepartmentName(String approveDepartmentName) {
		this.approveDepartmentName = approveDepartmentName;
	}

	@Temporal(TemporalType.DATE)
	@NotNull
	public Date getAllotedDate() {
		return allotedDate;
	}

	public void setAllotedDate(Date allotedDate) {
		this.allotedDate = allotedDate;
	}

	@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
	@Size(min = 1)
	public List<Image> getCertPhotos() {
		return certPhotos;
	}

	public void setCertPhotos(List<Image> certPhotos) {
		this.certPhotos = certPhotos;
	} 
	
	public boolean isHidden() {
		return hidden;
	}

	public void setHidden(boolean hidden) {
		this.hidden = hidden;
	}

	@Transient
	public void addEmptyQualificationTemplate() {
		QualificationTemplate template = new QualificationTemplate();
		template.setTemplateIndex(SystemUUIDUtil.getUUID());
		this.getQualificationTemplates().add(template);
	}
	
	@Transient
	public void removeQualificationTemplate(int index) {
		this.getQualificationTemplates().remove(index);
	}
	
	@Transient
	public String getCertIndex() {
		return certIndex;
	}

	public void setCertIndex(String certIndex) {
		this.certIndex = certIndex;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((auditResult == null) ? 0 : auditResult.hashCode());
		result = prime * result + ((endingValidityDate == null) ? 0 : endingValidityDate.hashCode());
		result = prime * result + ((serviceSupplier == null) ? 0 : serviceSupplier.hashCode());
		result = prime * result + (valid ? 1231 : 1237);
		result = prime * result + ((certIndex == null) ? 0 : certIndex.hashCode());
		return result;
	}

	

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		QualificationCert other = (QualificationCert) obj;		
		if (auditResult == null) {
			if (other.auditResult != null)
				return false;
		} else if (!auditResult.equals(other.auditResult))
			return false;
		if (endingValidityDate == null) {
			if (other.endingValidityDate != null)
				return false;
		} else if (!endingValidityDate.equals(other.endingValidityDate))
			return false;		
		if (serviceSupplier == null) {
			if (other.serviceSupplier != null)
				return false;
		} else if (!serviceSupplier.equals(other.serviceSupplier))
			return false;
		if (valid != other.valid)
			return false;
		if (certIndex != other.certIndex)
			return false;
		return true;
	}
}
